﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace WindowsImageCompressionApp.utils
{
    internal class DBUtil
    {

   
        /// <summary>
        /// 当前是 管理员权限
        /// </summary>
        /// <returns></returns>
        public static bool currentIsAdmin()
        {
            WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
            return isAdmin;
        }


        /// <summary>
        /// 窗口是否隐藏
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);


        // 获取当前进程名
        public static string getCurrentProcessName()
        {
            Process currentProcess = Process.GetCurrentProcess();
            string processName = currentProcess.ProcessName;

            return processName;
        }
        // 根据进程名终止进程
        public static void killProcessByName(string processName)
        {
            Process[] processes = Process.GetProcessesByName(processName);

            foreach (Process process in processes)
            {
                process.Kill();
            }
        }

        public static string getLocalUserName() => Environment.UserName;



        /// <summary>
        /// 文件体积转字符串
        /// </summary>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        public static string FormatFileSize(long fileSize)
        {
            string[] sizes = { "B", "KB", "MB", "GB", "TB" };
            int index = 0;
            double size = fileSize;

            while (size >= 1024 && index < sizes.Length - 1)
            {
                size /= 1024;
                index++;
            }

            return $"{size:0.##} {sizes[index]}";
        }

        /// <summary>
        /// 打开指定网址
        /// </summary>
        /// <param name="url"></param>
        public static void openLink(string url)
        {
            Process.Start(url);
        }

        /// <summary>
        /// 使用资源管理器 打开指定文件夹
        /// </summary>
        /// <param name="dir"></param>
        public static void openExplorerDir(string dir)
        {
            Process.Start("explorer.exe", dir);
        }
        /// <summary>
        /// 使用资源管理器 打开指定文件夹
        /// </summary>
        /// <param name="dir"></param>
        public static void openFile(string file)
        {
            if (File.Exists(file))
            {
                Process.Start(file);
            }
        }


    }
}
